<template>
  <div class="app-container">
    <Ht-search-form
      :defaultEventKey="defaultEventKey"
      :dictMap="dictMap"
      :queryParams="queryParams"
      :outputQueryParams="queryParams"
      :formOptions="formOptions"
      :advOptions="[]"
      permission="batch:timingConfig:view"
    ></Ht-search-form>
    <ht-table
      :defaultEventKey="defaultEventKey"
      ref="dataTable"
      :multi-select="true"
      :dictMap="dictMap"
      :reqConfig="reqConfig"
      :queryParams="queryParams"
      :tableBindColumns="tableBindColumns"
      :opButtonList="opButtonList"
      @handleSelectionChange="handleSelectionChange"
    >
      <template v-slot:operate>
        <el-table-column
          :label="$t('common.operate')"
          align="center"
          width="160"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <ht-button-group
              :buttonList="[
                {
                  label: $t('common.edit'),
                  permission: 'batch:timingConfig:edit',
                  doFunc: () => {
                    handleAction('update', scope.row);
                  },
                },
                {
                  label: $t('batch.timingConfig.form.launchParameters'),
                  permission: 'batch:timingConfig:edit',
                  doFunc: () => {
                    handleAction('params', scope.row);
                  },
                },
                {
                  label: $t('common.check'),
                  permission: 'batch:timingConfig:details',
                  doFunc: () => {
                    handleAction('details', scope.row);
                  },
                },
                {
                  label: $t('batch.timingConfig.form.executeOnce'),
                  permission: 'batch:timingConfig:test',
                  doFunc: () => {
                    handleAction('manualExecuteTask', scope.row);
                  },
                },
                {
                  label: $t('common.delete'),
                  permission: 'batch:timingConfig:remove',
                  doFunc: () => {
                    handleAction('del', scope.row);
                  },
                },
              ]"
            />
          </template>
        </el-table-column>
      </template>
    </ht-table>
    <!-- 编辑信息组件 -->
    <edit-config-info
      ref="editConfigDialog"
      @editComplete="editComplete"
    ></edit-config-info>

    <edit-timing-params ref="editTimingParams" />
  </div>
</template>
<script>
import {
  apiUrlConfig,
  del,
  manualExecuteTask,
} from "@/api/batch_app/timingConfig";
import { listPageMixin } from "@/libs/listPageMixin";
import editConfigInfo from "@/views/batch/timingConfig/compontents/EditTimingConfig";
import EditTimingParams from "@/views/batch/timingConfig/compontents/EditTimingParams";

export default {
  name: "ConfigInfo",
  components: { EditTimingParams, editConfigInfo },
  mixins: [listPageMixin],
  data() {
    return {
      defaultEventKey: "first",
      // 查询参数
      queryParams: {
        friendlyName: "",
      },
      // 搜索
      formOptions: [],
      reqConfig: apiUrlConfig,
      // 列信息
      tableBindColumns: [
        {
          label: this.$t("batch.missionManage.form.processCode"),
          prop: "flowCode",
        },
        {
          label: this.$t("batch.timingConfig.form.name"),
          prop: "friendlyName",
        },
        {
          label: this.$t("batch.timingConfig.form.executionTime"),
          prop: "timedExpression",
        },
        {
          label: this.$t("batch.timingConfig.form.expressionType"),
          prop: "expressionType",
          dictCode: "expression_type",
        },
        {
          label: this.$t("batch.missionManage.form.creationDate"),
          prop: "createTime",
        },
        {
          label: this.$t("batch.missionManage.form.updateTime"),
          prop: "updateTime",
        },
      ],
      opButtonList: () => {
        return [
          {
            label: this.$t("common.add"),
            type: "primary",
            icon: "el-icon-plus",
            permission: "batch:timingConfig:add",
            actionType: "add",
            clickFun: () => {
              this.handleAction("add");
            },
          },
        ];
      },
    };
  },
  mounted() {
    const formOptions = [
      {
        type: "input",
        prop: "friendlyName",
        defaultAttr: {
          size: "small",
          label: this.$t("batch.timingConfig.form.name"),
          placeholder: this.$t("batch.missionManage.rules.pleaseEnter"),
        },
      },
    ];
    this.formOptions = formOptions;
  },
  methods: {
    /***
     * 按钮操作汇总
     */
    handleAction(type, row = {}) {
      switch (type) {
        case "add":
          this.$refs.editConfigDialog.show();
          break;
        case "params":
          this.$refs.editTimingParams.show(row.id);
          break;
        case "manualExecuteTask":
          manualExecuteTask(row.id).then((res) => {
            if (res.result === true) {
              this.$message.success(
                this.$t("batch.timingConfig.form.executionSucceed")
              );
            } else {
              this.$message.error(
                this.$t("batch.timingConfig.form.executionFailed")
              );
            }
          });
          break;
        case "del":
          const id = row.id || this.ids.join(",");
          this.$confirm(
            this.$t("batch.systemConfig.rules.sureDelete"),
            this.$t("common.warning"),
            {
              confirmButtonText: this.$t("common.confirm"),
              cancelButtonText: this.$t("common.cancel"),
              type: "warning",
            }
          )
            .then(function () {
              return del(id);
            })
            .then(() => {
              this.doQuery(this.defaultEventKey);
              this.msgSuccess(this.$t("common.deleteSuccess"));
            })
            .catch((action) => {});
          break;
        case "update":
          this.$refs.editConfigDialog.show(row.id);
          break;
        case "details":
          this.$refs.editConfigDialog.show(row.id, true);
          break;
      }
    },
    // 弹出层操作完成回调 (可重写)
    editComplete(state) {
      console.log("edit complete", state);
      if (state) {
        // 编辑完成，刷新列表数据
        this.doQuery(this.defaultEventKey);
      }
    },
  },
};
</script>
